/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FuenteDeDatos;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultComboBoxModel;

public class DataCombobox  extends javax.swing.JComboBox implements Observer {
     private String campo;
     private String llave;
    private DataSource data=null;
    DefaultComboBoxModel modelo =new DefaultComboBoxModel() ;
    private List <String>lista=new ArrayList();
    //private datasource data2;



     public void conectar(){

         this.getData().addObserver(this);
        //getData().addObserver(this);

     }


    public void update(Observable o, Object o1) {
        
        //this.setText("");
        this.setModel(this.modelo);
        //this.setSelectedIndex(0);


        try {
            
             this.setModel(this.modelo);
        
           
           while(this.data.rs.next()){
               lista.add(this.data.rs.getString(this.getLlave()));
               modelo.addElement(this.data.rs.getString(this.getCampo()));
           }
            
       

           // data2 = (datasource) o;

           // this.setSelectedItem(this.data.rs.getString(this.getCampo()));

            //this.setText(this.data.consulta().getString(this.getCampo()));


        } catch (SQLException ex) {
            this.setSelectedIndex(0);
        }
    }


    public String getCampo() {
        return campo;
    }

    /**
     * @param campo the campo to set
     */
    public void setCampo(String campo) {
        this.campo = campo;
    }


    public DataSource getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(DataSource data) {
        this.data = data;
        if(data!=null){
            this.conectar();

        }
    }

    /**
     * @return the llave
     */
    public String getLlave() {
        return llave;
    }

    /**
     * @param llave the llave to set
     */
    public void setLlave(String llave) {
        this.llave = llave;
    }





}
